package com.example.toutiao.ui.main.frg

import android.content.Intent
import android.util.Log
import android.view.View
import androidx.core.view.isVisible
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.toutiao.R
import com.example.toutiao.base.BaseFragment
import com.example.toutiao.ui.main.act.LoginNoPassActivity
import com.example.toutiao.ui.main.act.UpdateUserInfoActivity
import com.example.toutiao.ui.main.adpater.CommonAdapter
import com.example.toutiao.ui.main.model.CommonItemModel
import com.example.toutiao.ui.main.model.CommonModel
import com.example.toutiao.ui.main.model.UserInfoModel
import com.example.toutiao.ui.main.presenter.MinePresenter
import com.example.toutiao.ui.main.view.MineView
import com.example.toutiao.ui.user.EvtMsgModel
import com.example.toutiao.utils.ImageLoaderUtils
import kotlinx.android.synthetic.main.fragment_mine.view.*

class MineFragment : BaseFragment<MineView, MinePresenter>(), MineView {

//    private var mCommonList: MutableList<CommonItemModel>?=ArrayList()
//    private var mCommonList1: MutableList<CommonItemModel>?=ArrayList()
    private var mList: MutableList<CommonModel>?=ArrayList()
    private var views:View?=null


    override fun getLayoutId(): Int = R.layout.fragment_mine

    override fun initView(view: View) {
        /**
         * 进入登录
         */

        /**
         * activity 是上下文
         * LinearLayoutManager 控制方向
         */
        /*构造数据*/
//        val item=CommonItemModel("我的关注","")
//        val item1=CommonItemModel("全民抽奖","")
//        mCommonList1!!.add(item1)
//        mCommonList!!.add(item)
//        val commonModel= CommonModel("常用", mCommonList!!)
//        val commonModel1= CommonModel("发现", mCommonList1!!)
//        mList!!.add(commonModel)
//        mList!!.add(commonModel1)
        views=view
        views!!.mine_login.setOnClickListener{

            startActivity(LoginNoPassActivity::class.java)
        }
        views!!.rl_show_user.setOnClickListener {
            startActivity(UpdateUserInfoActivity::class.java)
        }



    }

    override fun initData() {
        getPresenter()!!.getCommList(1)
        if(isTokenNotNull()){
            getPresenter()!!.getUserInfo(getToken()!!)
        }else{
            views!!.rl_show_user.isVisible=false
            views!!.login_box.isVisible=true
        }
    }

    override fun updateUserInfo(mUserInfo: UserInfoModel) {
        if(mUserInfo!=null){
            views!!.rl_show_user.isVisible=true
            views!!.login_box.isVisible=false
            ImageLoaderUtils.loadCircleImage(requireActivity(),mUserInfo.user_url,views!!.avatar)
            views!!.nickname.text=mUserInfo.user_login
            views!!.tv_number.text="粉丝 ${mUserInfo.releaseNum} 关注 ${mUserInfo.followNum} 动态 ${mUserInfo.fansNum}"
        }
    }


    override fun createPresenter(): MinePresenter = MinePresenter()

    override fun isRegister(): Boolean = true
    override fun getMessage(message: EvtMsgModel<*>) {
        super.getMessage(message)
        Log.e("event", message.code.toString())
        if(message!=null){
            if(message.code==201){
                val token=message.msg as String
                getPresenter()!!.getUserInfo(token)
            }
        }
    }


    override fun <T> setData(data: T) {
        if(data!=null){
           mList=data as MutableList<CommonModel>
            views!!.rv_common_list.layoutManager=LinearLayoutManager(activity)
            views!!.rv_common_list.adapter= CommonAdapter(mList!!)
        }
    }

    override fun setError(err: String) {

    }

}